<?php

/*
 * Controller/ProfilController.php
 */

namespace RHX\Controller;

class ProfilController extends RHXController {

    public function indexAction() {
	return array(
	    'profilList' => $this->getService('Table\ProfilTable')->getAll(),
	);
    }

    /**
     * Ajouter un nouveau profil
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function addAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// POST
	$post		 = $this->getRequest()
		->getPost();
	// Nouveau profil
	$profil		 = new \RHX\Model\Object\Profil($post);
	// table Profil
	$tableProfil	 = new \RHX\Model\Table\ProfilTable($this->getAdapter());
	// Sauvegarde du profil
	$id		 = $tableProfil->save($profil);
	$profil->id	 = $id;

	return new \Zend\View\Model\JsonModel(array(
	    'profil' => $profil,
	    'done'	 => ($id != 0) ? true : false,
	));

	return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'profil', 'action'	 => 'index'));
    }

    public function editAction() {
	$request = $this->getRequest();
	$profil	 = new \RH\Model\Profil();
	$profil->exchangeArray($request->getPost());
	$this->getService('RH\Model\Table\ProfilTable')
		->save($profil);

	return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'profil', 'action'	 => 'index'));
    }

    public function eraseAction() {
	// table Profil
	$tableProfil	 = new \RHX\Model\Table\ProfilTable($this->getAdapter());
	// POST
	$post		 = $this->getRequest()
		->getPost();
	$nb		 = $tableProfil->erase($post->id);

	return new \Zend\View\Model\JsonModel(array(
	    'post'	 => $post,
	    'id'	 => $post->id,
	    'nb'	 => $nb,
	));

	return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'profil', 'action'	 => 'index'));
    }

    public function saveStringDataAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// Adapteur
	$adapter = $this->getAdapter();
	// POST
	$post	 = $this->getRequest()
		->getPost();
	// Variables
	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);
	// Requête SQL
	$sql	 = "UPDATE rh_profil SET $name = '$value' WHERE id = $pk";
	$result	 = $adapter->query($sql)
		->execute();
	$data	 = array(
	    'done'	 => false,
	    'error'	 => null,
	);
	if ($result) {
	    $data['done'] = true;
	}
	else {
	    $data['error'] = mysql_error();
	}

	return new \Zend\View\Model\JsonModel($data);
    }

    public function saveIntDataAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// Adapteur
	$adapter = $this->getAdapter();
	// POST
	$post	 = $this->getRequest()
		->getPost();
	// Variables
	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = $post->value;
	// Requête SQL
	$sql	 = "UPDATE rh_profil SET $name = $value WHERE id = $pk";
	$result	 = $adapter->query($sql)
		->execute();
	$data	 = array(
	    'done'	 => false,
	    'error'	 => null,
	);
	if ($result) {
	    $data['done'] = true;
	}
	else {
	    $data['error'] = mysql_error();
	}

	return new \Zend\View\Model\JsonModel($data);
    }

}

?>
